#os.getcwd() mostra o direitorio corrente
#os.listdir(os.getcwd()) mostra todos os diretorios e arquivos do diretorio passado como parametro
#os.chdir(os.getcwd()) muda para o diretorio passado como parametro

############### Encontrar uma palavra ou frase em um arquivo ####################
#
# Entrada
#    nome
# Saida
#   nome do arquivo e as linhas a qual o nome esta presente
#
#################################################################################

import os
import sys

DIR_LOCAL = os.getcwd()
STRING = " ".join(sys.argv[1:])

found = open("files_found.txt","w")

def findString(string, file_text):
	
	lines = []
	try:
		file_open = open(file_text).readlines()
	except:
		return lines

	for line in range(len(file_open)):
		
		if string in file_open[line]:
			lines.append((line,file_open[line]))
		
	return lines

def isDirectory(DIR):

	if DIR[0] == ".":
		return False

	try:
		os.listdir(DIR)
	except:		
		return False

	return True


def deepDir(DIR,string):
	
	files = os.listdir(DIR)

	for directory in files:
		
		if isDirectory(directory):		
			deepDir(DIR+"/"+directory,string)
		else:
			result = findString(string, DIR+"/"+directory)
			if len(result) > 0:
				found.write(DIR+"/"+directory+"\n"+"-"*20+"\n")
				found.write("Lines-")
				for line in range(len(result)):
					found.write(str(result[line][0])+"--->"+result[line][1]+"\n")




deepDir(DIR_LOCAL,STRING)
